package com.kang.controller;


import com.github.pagehelper.PageInfo;
import com.kang.bean.Play;
import com.kang.bean.PlayInfo;
import com.kang.commons.Result;
import com.kang.commons.util.ValidationUtil;
import com.kang.service.PlayService;
import com.kang.vo.PlayVo;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("plays")
@ResponseBody
@Slf4j
public class PlayController {

    @Autowired
    private PlayService playService;

    /**
     * 添加游乐设施的图片
     */
    @PostMapping("addPic")
    public Result addPic(@RequestParam MultipartFile file) throws Exception {
        String path = playService.addPic(file);
        return Result.success(path);
    }

    /**
     * 添加游乐设施
     */
    @PostMapping("/addPlay")
    public Result addPlay(@RequestBody Play playVo){
        String str = playVo.getPicid();
        //添加时间
        playVo.setDate(new Date(System.currentTimeMillis()));
        playVo.setRunning(1);
        playVo.setStatusid(0);
        try{
            PlayInfo playInfo = new PlayInfo();
            playInfo.setInfo(playVo.getInfo());
            playInfo.setSuitable(playVo.getSuitable());
            playInfo.setStimulus(playVo.getStimulus());
            playService.addPlay(playVo);
            log.info("添加一条游乐设施信息 - {}",playVo.getName());
            return Result.success("添加成功");
        }catch(Exception e) {
            e.printStackTrace();
            log.error("添加游乐设施失败 - {}",playVo.getName());
            return Result.error("添加失败！");
        }
    }

    /**
     * 分页查询游乐设施
     * @return
     */
    @PostMapping("/public/getList")
    public Result queryList(Pageable page, @RequestBody PlayVo playVo){
        log.info("分页条件查询：页码 - {} 每页记录数 - {} 条件 - {}", page.getPageNumber(), page.getPageSize(), playVo);
        if(StringUtils.isNotEmpty(playVo.getStartDate()) && playVo.getStartDate().contains(";")) {
            String[] data = playVo.getStartDate().split(";");
            playVo.setStartDate(data[0]);
            playVo.setEndDate(data[1]);
        }
        PageInfo<PlayVo> playPage =  playService.queryList(page,playVo);
        if(ObjectUtils.isEmpty(playPage)){
            return Result.error("系统中无设备信息！");
        }
        return Result.success(playPage);
    }
//
//    @DeleteMapping("{id}")
//    public Result delPlay(@PathVariable String id){
//        playService.delById(id);
//        return Result.success("删除成功！");
//    }
//
    /**
     * 修改设施信息方法
     * @param play
     * @return
     */
    @PostMapping
    public Result updatePlay(@RequestBody Play play){
        // 校验设备参数
        ValidationUtil.validate(play);
        log.info("修改游乐设施 - {}", play.getName());
        playService.updatePlay(play);
        return Result.success("修改成功！");
    }

}
